// 分类数据相关的逻辑
import { ref ,onMounted} from 'vue'
import { getTopCategoryAPI } from '@/apis/category'
import {useRoute} from 'vue-router'
import {onBeforeRouteUpdate} from 'vue-router'
export function useCategory() {
  // 获取顶级分类数据
    const categoryData = ref({})
    const route = useRoute()
    const getTopCategory = async (id = route.params.id) => {
      const res = await getTopCategoryAPI(id)
    
      categoryData.value = res.result
    }
    onMounted(() => {
      getTopCategory()
    })

    onBeforeRouteUpdate((to) => {
      getTopCategory(to.params.id)
    })
    return {
      categoryData
    }
}
